<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Busines;
use Illuminate\Support\Facades\Bus;

class BussController extends Controller
{
    //业务添加
    public function business_add(Request $request,Busines $busines){
        if($request->isMethod('post')){
            $data = $request->except('_token');
            $img = $request->file('img')->store(date('Y-m-d'));
            $data['img'] = $img;
            $data['content'] = $data['editorValue'];
            $result = $busines->create($data);
            if($result != false){
                session()->flash('success','添加成功');
                return redirect()->back();
            }else{
                session()->flash('success'.'添加失败');
                return redirect()->back();
            }
        }else{
            return view('Admin/buss/business_add');
        }
    }
    //列表
    public function business_list(Busines $busines){
        $data = $busines->get();
        return view('Admin/buss/business_list',['data'=>$data]);
    }
    //修改
    public function business_upd(Busines $busines,$id){
        $data = $busines->where('id',$id)->get();
        return view('Admin/buss/business_upd',['data'=>$data]);
    }
    //修改
    public function update(Busines $busines,Request $request){
        $data = $request->except('_token');
        $where = ['id' => $data['id']];
        if(!empty($data['editorValue'])){
            $re['content'] = $data['editorValue'];
        }
        if(!is_null($request->file('img'))){
            $re['img'] = $request->file('img')->store(date('Y-m-d'));
        }
        $re['name'] = $data['name'];
        $re['recomm'] = $data['recomm'];
        $re['tell'] = $data['tell'];
        $result = $busines->where($where)->update($re);
        if($result != false){
            session()->flash('success','修改成功');
            return redirect()->back();
        }else{
            session()->flash('success','修改失败');
            return redirect()->back();
        }
    }
    //删除
    public function delete($id){
        $busines = new Busines();
        $result = $busines->where('id',$id)->delete();
        return redirect()->back();
    }
}
